let storage = bui.storage();
let language,translate;
loader.define(function (require, exports, module, global) {
    let pageview = {
        init() {
            translate = global.translate
            language = storage.get("language", 0)
            if (!language) {
                language = 'chinese_simplified'
            }

            try {
                translate.selectLanguageTag.show = false;
                translate.ignore.id.push('languageContent')
                let curLanguage = translate.language.getCurrent()
                if (curLanguage !== language) {
                    translate.changeLanguage(language)
                }
                translate.execute();
            }catch (e) {
                console.log("切换语言报错")
            }
            let languageArr = storage.get('languageArr',0)
            let html = ''
            let html2 = '<select class="languageSelect" id="languageSelectId">\n'
            if (language !== 'chinese_simplified' && language !== 'english') {
                languageArr.forEach(lan => {
                    if (lan.id === language) {
                        html += '<span class="language" id="'+lan.id+'">'+lan.name+'</span>|'
                    } else {
                        html2 += '<option value="'+lan.id+'">'+lan.name+'</option>'
                    }
                })
            } else {
                languageArr.forEach(lan => {
                    html2 += '<option value="'+lan.id+'">'+lan.name+'</option>'
                })
            }
            html+= '<span class="language" id="english">English</span>|<span class="language" id="chinese_simplified">简体中文</span>'
            router.$("#languageContent").html(html + html2 + "</select>")

            router.$(".language").click(function () {
                let selectId = $(this).attr('id')
                if (selectId !== language) {
                    storage.set("language", selectId)
                    location.reload()
                }
            })
            router.$("#languageSelectId").val("");
            router.$("#languageSelectId").change(function () {
                let selectId = $(this).val()
                if (selectId !== language) {
                    storage.set("language", selectId)
                    location.reload()
                }
            })

            let uiLoading = bui.loading({
                appendTo:"#loadingAuditComment",
                width: 40,
                height: 40,
                opacity: 0.5,
                autoClose: false,
                text:"提交中。。。",
                callback: function (argument) {
                }
            });
            let uiLoading2 = bui.loading({
                appendTo:"#loadingAuditComment",
                width: 40,
                height: 40,
                opacity: 0.5,
                autoClose: false,
                text:"加载中。。。",
                callback: function (argument) {
                }
            });

            const url = window.location.href
            let param = url.split("?")[1]
            let id
            if (param) {
                id = param.split("=")[1]
            }
            if (!id) {
                id = storage.get("commentId", 0)
            }
            let user;
            let token = storage.get("token",0)

            if (token) {
                uiLoading2.show()
                bui.ajax({
                    url: global.api.verifyToken,
                    method: "POST",
                    headers: {Token: token}
                }).then(function(res) {
                    if (res.code != '200') {
                        uiLoading2.stop()
                        bui.confirm({
                            "title": "",
                            "height": 460,
                            "content": '<div class="bui-box-center"><div><h3>登录已过期</h3></div></div>',
                            "buttons": [{ name: "确定", className: "primary-reverse" }],
                            callback:function(){
                                this.close();
                                storage.set("dlTz", '17')
                                storage.set("commentId", id)
                                router.replace({ url: "pages/login/login.html", param:{tzSign: '0'}});
                            }
                        });
                        try {
                            translate.execute();
                        }catch (e) {
                            console.log("切换语言报错")
                        }
                    }
                })

                //加载评论详情
                bui.ajax({
                    url: global.api.getComment,
                    data: {id: id},
                    method: "POST"
                }).then(function(res){
                    let data = res.data
                    user = data.user

                    router.$("#commentArticle").html(data.pidStr)
                    router.$("#commentArticle").click(function () {
                        storage.set("articleLookId", data.pid)
                        storage.set("commentId", id)
                        router.load({ url: "pages/articleLook/articleLook.html", param: {tzSign: '1'}});
                    })
                    router.$("#commentUser").html(data.userStr)
                    router.$("#commentContent").html(data.content)

                    if (data.status != '2') {
                        let titleStr = ''
                        if(data.status == '3'){
                            titleStr = "该评论已审核失败，确定要重更审核么?"
                        } else {
                            titleStr = "该评论已审核成功，确定要重更审核么?"
                        }
                        bui.confirm(titleStr, function(e) {
                            //this 是指点击的按钮
                            let text = $(e.target).text();

                            if (text == "取消") {
                                storage.set("tzPage", 0)
                                router.replace({ url: "pages/mainIndex/mainIndex.html"});
                            }

                            this.close()
                        });
                    }
                    uiLoading2.stop()

                    try {
                        translate.execute();
                    }catch (e) {
                        console.log("切换语言报错")
                    }
                })
            } else {
                bui.confirm({
                    "title": "",
                    "height": 460,
                    "content": '<div class="bui-box-center"><div><h3>当前未登录，请登录</h3></div></div>',
                    "buttons": [{ name: "登录", className: "primary-reverse" }],
                    callback:function(){
                        this.close();
                        storage.set("dlTz", '17')
                        storage.set("commentId", id)
                        router.replace({ url: "pages/login/login.html", param:{tzSign: '0'}});
                    }
                });
                try {
                    translate.execute();
                }catch (e) {
                    console.log("切换语言报错")
                }
            }

            router.$("#refause").click(function () {
                // bui.prompt("请输入拒绝原因",function(e){
                //     var text = router.$(e.target).text();
                //     if( text == "取消"){
                //         this.close();
                //     }else{
                //         let thi = this
                //         bui.ajax({
                //             url: global.api.auditComment,
                //             data: {id: id, status: '3', error: this.value(), user: user},
                //             method: "POST"
                //         }).then(function(res){
                //             if (res.code == '200') {
                //                 thi.close();
                //                 storage.set("tzPage", 0)
                //                 bui.load({ cache:false, url: "pages/mainIndex/mainIndex.html", replace: true});
                //             } else {
                //                 bui.alert(res.msg)
                //             }
                //         })
                //     }
                // })

                bui.confirm("确定提交审核拒绝么?", function(e) {
                    //this 是指点击的按钮
                    let text = $(e.target).text();

                    if (text == "确定") {
                        uiLoading.show()
                        let thi = this
                        bui.ajax({
                            url: global.api.auditComment,
                            data: {id: id, status: '3', user: user},
                            method: "POST"
                        }).then(function(res){
                            if (res.code == '200') {
                                thi.close();
                                storage.set("tzPage", 0)
                                router.replace({ url: "pages/mainIndex/mainIndex.html"});
                            } else {
                                bui.alert('操作失败')
                            }
                            uiLoading.stop()
                        })
                    } else {
                        this.close()
                    }
                });
            })
            router.$("#success").click(function () {
                bui.confirm("确定提交审核成功么?", function(e) {
                    //this 是指点击的按钮
                    let text = $(e.target).text();

                    if (text == "确定") {
                        uiLoading.show()
                        let thi = this
                        bui.ajax({
                            url: global.api.auditComment,
                            data: {id: id, status: '1', user: user},
                            method: "POST"
                        }).then(function(res){
                            if (res.code == '200') {
                                thi.close();
                                storage.set("tzPage", 0)
                                router.replace({ url: "pages/mainIndex/mainIndex.html"});
                            } else {
                                bui.alert('操作失败')
                            }
                            uiLoading.stop()
                        })
                    } else {
                        this.close()
                    }
                });
            })
        }
    };

    // 初始化
    pageview.init();

    // 输出模块
    return pageview;
})
